/* $RCSFile: LanguageCultureItem.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/Helpers/LanguageCultureItem.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:28  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Globalization;
using System.Collections;

namespace AddonNice.Helpers
{
	/// <summary>
	/// Single item in list. Language culture pair.
	/// </summary>
	public class LanguageCultureItem
	{
		private CultureInfo m_UICulture;
		private CultureInfo m_culture;

		public LanguageCultureItem(CultureInfo uiCulture, CultureInfo culture)
		{
			if (uiCulture == null) 
				UICulture = CultureInfo.InvariantCulture;
			else
				UICulture = uiCulture;

			if (culture == null) 
				Culture = CultureInfo.InvariantCulture;
			else
				Culture = culture;
		}

		public LanguageCultureItem()
		{
			UICulture = CultureInfo.InvariantCulture;
			Culture = CultureInfo.CreateSpecificCulture(CultureInfo.InvariantCulture.Name);
		}

		public CultureInfo UICulture
		{
			get
			{
				return m_UICulture;
			}
			set
			{
				m_UICulture = value;
			}
		}

		public CultureInfo Culture
		{
			get
			{
				return m_culture;
			}
			set
			{
				if (value.IsNeutralCulture)
					throw new ArgumentException("Culture value cannot be neutral", "Culture");

				m_culture = value;
			}
		}

		public override string ToString()
		{
			return UICulture.Name + "/" + Culture.Name;
		}

		public static implicit operator string (LanguageCultureItem item)
		{
			return item.ToString();
		}

//		public static bool operator==(LanguageCultureItem a, LanguageCultureItem b) 
//		{
//			return LanguageCultureItem.Equals(a, b);
//		}

//		public static bool operator!=(LanguageCultureItem a, LanguageCultureItem b) 
//		{
//			return !LanguageCultureItem.Equals(a, b);
//		}

		public override bool Equals(Object obj)
		{
			if (obj.GetType() == typeof(LanguageCultureItem))
			{
				return Equals(this, (LanguageCultureItem) obj);
			}
			else
			{
				return false;
			}
		}

		public static bool Equals(LanguageCultureItem a, LanguageCultureItem b)
		{
			if ((a == null) || (b == null))
			{
				return false; 
			}
			if (a.ToString() == b.ToString())
			{
				return true; 
			}
			return a.Equals(b); 
		} 

		/// <summary>
		/// We must override GetHashCode when we override Equals
		/// </summary>
		/// <returns></returns>
		public override Int32 GetHashCode() 
		{
			return (Int32) ((UICulture.LCID * 5000) + Culture.LCID);
		}
	}
}
